import pygame, sys

clock = pygame.time.Clock()

from pygame.locals import *
pygame.init() # initialize pygame

pygame.display.set_caption("My game!") #pygame window's caption

WINDOW_SIZE = (600, 400)

window = pygame.display.set_mode(WINDOW_SIZE) # set main window size

display = pygame.Surface((300, 200))


def read_map():
	file = open('game_map.txt', "r")

	lines = file.read()

	file.close()

	lines = lines.split("\n")

	game_map = []
	
	for line in lines:
		game_map.append(list(line))

	return game_map

game_map = read_map()

player = pygame.image.load("images/my_player.png") # load external images - player

#Converting white color on the image into transparent background
# In my case this is the white color
player.set_colorkey((255,255,255))

dirt = pygame.image.load("images/my_dirt.png")
grass = pygame.image.load("images/my_grass.png")
grass_right = pygame.image.load("images/my_grass_r.png")
grass_left = pygame.image.load("images/my_grass_l.png")

tile_width = grass.get_width()

def collisiton_test(player_rect, tiles):
	hit_list = []
	for tile in tiles:
		if player_rect.colliderect(tile):
			hit_list.append(tile)
	return hit_list

def move(player_rect, player_movement, tiles_hit):
	collision_types = {"top":False, "bottom":False, "right":False, "left":False}
	player_rect.x += player_movement[0]
	hit_list = collisiton_test(player_rect,tiles_hit)	
	for tile in hit_list:
		if player_movement[0] > 0:
			player_rect.right = tile.left
			collision_types["right"] = True
		elif player_movement[0] < 0:
			player_rect.left = tile.right
			collision_types["left"] = True

	player_rect.y += player_movement[1]
	hit_list = collisiton_test(player_rect, tiles_hit)
	for tile in hit_list:
		if player_movement[1] > 0:
			player_rect.bottom = tile.top
			collision_types["bottom"] = True
		elif player_movement[1] < 0:
			player_rect.top = tile.bottom
			collision_types["top"] = True

	return player_rect, collision_types

move_right = False
move_left = False

player_y_momentum = 0
air_time = 0

player_position = [50, 50]

player_rect = pygame.Rect(player_position[0], player_position[1], player.get_width(), player.get_height()) 
test_rect = pygame.Rect(50,50,100,50)

true_scroll = [0, 0]

# [[layer], [x,y,width,height]]
background_objects = [[0.25,[120,10,70,400]],[0.25,[280,30,40,400]],[0.5,[30,40,40,400]],[0.5,[130,90,100,400]],[0.5,[300,80,120,400]], [0.75, [10,0,100,500]]]


while True:
	display.fill((146, 244,255)) # fill main window with blue color

	true_scroll[0] += (player_rect.x-true_scroll[0]-152)/20
	true_scroll[1] += (player_rect.y-true_scroll[1]-106)/20
	scroll = true_scroll.copy()
	scroll[0] = int(scroll[0])
	scroll[1] = int(scroll[1])

	pygame.draw.rect(display, (7,80,75),pygame.Rect(0,120,300,80))
	for background_object in background_objects:
		layer = background_object[0]
		obj_rect = pygame.Rect(background_object[1][0]-scroll[0]*layer,background_object[1][1]-scroll[1]*layer,background_object[1][2],background_object[1][3])
		if layer == 0.5:
			pygame.draw.rect(display, (14,222,150), obj_rect)
		if layer == 0.75:
			pygame.draw.rect(display, (100,100,100), obj_rect)
		else:
			pygame.draw.rect(display, (9,91,85), obj_rect)

	tile_rects = []
	y = 0
	for row in game_map:
		x = 0
		for cell in row:
			if cell == "1":
				display.blit(dirt, (x*tile_width-scroll[0], y*tile_width-scroll[1]))
			if cell == "2":
				display.blit(grass, (x*tile_width-scroll[0], y*tile_width-scroll[1]))
			if cell == "3":
				display.blit(grass_right, (x*tile_width-scroll[0], y*tile_width-scroll[1]))
			if cell == "4":
				display.blit(grass_left, (x*tile_width-scroll[0], y*tile_width-scroll[1]))
			if cell != "0":
				tile_rects.append(pygame.Rect(x*tile_width, y*tile_width, tile_width, tile_width))
			x += 1
		y += 1

	player_movement = [0, 0]
	if move_right:
		player_movement[0] += 4
	if move_left:
		player_movement[0] -= 4
	player_movement[1] += player_y_momentum
	player_y_momentum += 0.2
	if player_y_momentum > 3:
		player_y_momentum = 3

	player_rect, collisions = move(player_rect, player_movement, tile_rects)

	if collisions["bottom"]:
		player_y_momentum = 0
		air_time = 0
	else:
		air_time += 1

	if collisions["top"]:
		player_y_momentum = 0

	display.blit(player, (player_rect.x-scroll[0], player_rect.y-scroll[1]))

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()

		if event.type == KEYDOWN:
			if event.key == K_RIGHT:
				move_right = True
			if event.key == K_LEFT:
				move_left = True
			if event.key == K_UP:
				if air_time < 6:
					player_y_momentum = -5
		if event.type ==  KEYUP:
			if event.key == K_RIGHT:
				move_right = False
			if event.key == K_LEFT:
				move_left = False


	surface = pygame.transform.scale(display, WINDOW_SIZE)
	window.blit(surface, (0, 0))
	pygame.display.update()
	# FPS
	clock.tick(60)